home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / TEST_ITE.C < prev    next >
C/C++ Source or Header  |  1992-09-29  |  8KB  |  201 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Updated: JAM 08/18/92 -- modernize template syntax, remove macro hacks
  13.  
  14. #include <cool/String.h>
  15. #include <cool/Iterator.h>
  16. #include <cool/Vector.h>
  17. #include <cool/Association.h>
  18. #include <cool/Queue.h>
  19. #include <cool/List.h>
  20. #include <cool/Set.h>
  21. #include <cool/Bit_Set.h>
  22. #include <cool/Hash_Table.h>
  23. //#include <cool/Package.h>
  24. #include <test.h>
  25.  
  26. #include <cool/Vector.C>
  27. #include <cool/Pair.C>
  28. #include <cool/Association.C>
  29. #include <cool/Queue.C>
  30. #include <cool/List.C>
  31. #include <cool/Set.C>
  32. #include <cool/Hash_Table.C>
  33.  
  34. void test_Iter_Vector() {
  35.   CoolVector<int> v(10);
  36.   TEST ("CoolVector<int> v(10)", v.capacity(), 10);
  37.   CoolIterator<CoolVector<int> > iv;
  38.   TEST ("CoolIterator<CoolVector> iv", 1, 1);
  39.   TEST_RUN ("for (int i=0;i<10;i++) v.push(i)",
  40.         for (int i = 0; i < 10; i++) v.push(i), v.length(), 10);
  41.   TEST ("v.reset()", (v.reset(), 1),1);
  42.   TEST ("iv = v.current_position()", (iv = v.current_position(), iv), INVALID);
  43.   TEST ("v.next; v.value()==0;", (v.next (), v.value()), 0);
  44.   TEST ("v.next; v.value()==1;", (v.next (), v.value()), 1);
  45.   TEST ("v.current_position() = iv", (v.current_position()=iv, iv), INVALID);
  46.   TEST ("v.next(); v.value()==0;", (v.next(),v.value()), 0);
  47. }
  48.  
  49. void test_Iter_Association() {
  50.   typedef CoolPair<int,char*> KVHack;
  51.   CoolAssociation<int,char*> a(10);
  52.   TEST ("CoolAssociation<int,char*> a(10)", a.capacity(), 10);
  53.   CoolIterator<CoolAssociation<int,char*> > ia;
  54.   TEST ("CoolIterator<CoolAssociation> ia", 1, 1);
  55.   TEST_RUN ("for (int i=0;i<10;i++) a.put(i,\"ABC\")",
  56.         for (int i = 0; i < 10; i++) a.put(i, "ABC"), a.length(), 10);
  57.   TEST ("a.reset()", (a.reset(), 1),1);
  58.   TEST ("ia = a.current_position()", (ia = a.current_position(), ia), INVALID);
  59.   TEST ("a.next; a.value()==(0,\"ABC\");", 
  60.     (a.next (), (a.key()==0 && !strcmp(a.value(),"ABC"))), 1);
  61.   TEST ("a.next; a.value()==(1,\"ABC\");", 
  62.     (a.next (), (a.key()==1 && !strcmp(a.value(),"ABC"))), 1);
  63.   TEST ("a.current_position() = ia", (a.current_position()=ia, ia), INVALID);
  64.   TEST ("a.next; a.value()==(0,\"ABC\");", 
  65.     (a.next (), (a.key()==0 && !strcmp(a.value(),"ABC"))), 1);
  66. }
  67.  
  68. void test_Iter_List() {
  69.   CoolList<int> l;
  70.   TEST ("CoolList<int> l", 1, 1);
  71.   CoolIterator<CoolList<int> > il;
  72.   TEST ("CoolIterator<CoolList> il", 1, 1);
  73.   TEST_RUN ("for (int i=0;i<10;i++) l.push(i)",
  74.         for (int i = 0; i < 10; i++) l.push(i), l.length(), 10);
  75.   TEST ("l.reset()", (l.reset(), 1),1);
  76.   TEST ("il = l.current_position()", (il = l.current_position(), il), NULL);
  77.   TEST ("l.next; l.value()==9;", (l.next (), l.value()), 9);
  78.   TEST ("l.next; l.value()==8;", (l.next (), l.value()), 8);
  79.   TEST ("l.current_position() = il", (l.current_position()=il, il), NULL);
  80.   TEST ("l.next(); l.value()==9;", (l.next(),l.value()), 9);
  81. }
  82.  
  83. void test_Iter_Queue() {
  84.   CoolQueue<int> q(10);
  85.   TEST ("CoolQueue<int> q(10)", q.capacity(), 10);
  86.   CoolIterator<CoolQueue<int> > iq;
  87.   TEST ("CoolIterator<CoolQueue> iq", 1, 1);
  88.   TEST_RUN ("for (int i=0;i<10;i++) q.put(i)",
  89.         for (int i = 0; i < 10; i++) q.put(i),q.length(), 10);
  90.   TEST ("q.reset()", (q.reset(), 1),1);
  91.   TEST ("iq = q.current_position()", (iq = q.current_position(), 0), 0);
  92.   TEST ("q.next; q.value()==0;", (q.next (), q.value()), 0);
  93.   TEST ("q.next; q.value()==1;", (q.next (), q.value()), 1);
  94.   TEST ("q.current_position() = iq", (q.current_position()=iq, 0), 0);
  95.   TEST ("q.next(); q.value()==0;", (q.next(),q.value()), 0);
  96. }
  97.  
  98.  
  99. void test_Iter_Set() {
  100.   CoolSet<int> s(10);
  101.   TEST ("CoolSet<int> s(10)", (s.capacity() >= 10), 1);
  102.   CoolIterator<CoolSet<int> > is;
  103.   TEST ("CoolIterator<CoolSet> is", 1, 1);
  104.   TEST_RUN ("for (int i=0;i<10;i++) s.put(i)",
  105.         for (int i = 0; i < 10; i++) s.put(i),s.length(), 10);
  106.   TEST ("s.reset()", (s.reset(), 1),1);
  107.   TEST ("is = s.current_position()", (is = s.current_position(), is), INVALID);
  108.   TEST ("s.next; s.value()==0;", (s.next (), s.value()), 0);
  109.   TEST ("s.next; s.value()==1;", (s.next (), s.value()), 1);
  110.   TEST ("s.current_position() = is", (s.current_position()=is, is), INVALID);
  111.   TEST ("s.next(); s.value()==0;", (s.next(),s.value()), 0);
  112.  }
  113.  
  114. void test_Iter_Bit_Set() {
  115.   CoolBit_Set b(10);
  116.   TEST ("CoolBit_Set b(10)", (b.capacity() >= 10), 1);
  117.   CoolIterator<CoolBit_Set> ib;
  118.   TEST ("CoolIterator<CoolBit_Set> ib", 1, 1);
  119.   TEST_RUN ("for (int i=0;i<10;i++) b.put(i)",
  120.         for (int i = 0; i < 10; i++) b.put(i),b.length(), 10);
  121.   TEST ("b.reset()", (b.reset(), 1),1);
  122.   TEST ("ib = b.current_position()", (ib = b.current_position(), ib), INVALID);
  123.   TEST ("b.next; b.value()==0;", (b.next (), b.value()), 0);
  124.   TEST ("b.next; b.value()==1;", (b.next (), b.value()), 1);
  125.   TEST ("b.current_position() = ib", (b.current_position()=ib, ib), INVALID);
  126.   TEST ("b.next(); b.value()==0;", (b.next(),b.value()), 0);
  127. }
  128.  
  129. void test_Iter_Hash_Table() {
  130.   CoolHash_Table<int,CoolString> h(10);
  131.   TEST ("CoolHash_Table<int,CoolString> h(10)", (h.capacity() >= 10), 1);
  132.   CoolIterator<CoolHash_Table<int,CoolString> > ih;
  133.   TEST ("CoolIterator<CoolHash_Table> ih", 1, 1);
  134.   TEST_RUN ("for (int i=0;i<10;i++) h.put(i,CoolString(\"ABC\"))",
  135.         for (int i=0; i < 10; i++) h.put(i, CoolString("ABC")),h.length(), 10);
  136.   TEST ("h.reset()", (h.reset(), 1),1);
  137.   TEST ("ih = h.current_position()", (ih = h.current_position(), ih), INVALID);
  138.   TEST ("h.next; h.value()==(0,CoolString(\"ABC\"));", 
  139.     (h.next (), (h.key()==0 && !strcmp(h.value(),"ABC"))), 1);
  140.   TEST ("h.next; h.value()==(1,CoolString(\"ABC\"));", 
  141.     (h.next (), (h.key()==1 && !strcmp(h.value(),"ABC"))), 1);
  142.   TEST ("h.current_position() = ih", (h.current_position()=ih, ih), INVALID);
  143.   TEST ("h.next; h.value()==(0,CoolString(\"ABC\"));", 
  144.     (h.next (), (h.key()==0 && !strcmp(h.value(),"ABC"))), 1);
  145.  }
  146. /*
  147. void test_Iter_Package() {
  148.   Package p(10);
  149.   TEST ("Package p(10)", (p.capacity() >= 10), 1);
  150.   CoolIterator<Package> ip;
  151.   TEST ("CoolIterator<Package> ip", 1, 1);
  152.   Symbol* sym_a = p.intern("A");
  153.   Symbol* sym_b = p.intern("B");
  154.   Symbol* sym_c = p.intern("C");
  155.   Symbol* sym_d = p.intern("D");
  156.   Symbol* sym_e = p.intern("E");
  157.   Symbol* sym_f = p.intern("F");
  158.   Symbol* sym_g = p.intern("G");
  159.   Symbol* sym_h = p.intern("H");
  160.   Symbol* sym_i = p.intern("I");
  161.   Symbol* sym_j = p.intern("J");
  162.   TEST ("p.intern(\"ABCDEFGHIJ\")",p.length(), 10);
  163.   TEST ("p.reset()", (p.reset(), 1),1);
  164.   TEST ("ip = p.current_position()", (ip = p.current_position(), ip), INVALID);
  165.   TEST ("p.next; p.value()", (p.next (), p.value()), sym_b);
  166.   TEST ("p.next; p.value()", (p.next (), p.value()), sym_e);
  167.   TEST ("p.current_position() = ip", (p.current_position()=ip, ip), INVALID);
  168.   TEST ("p.next; p.value()", (p.next (), p.value()), sym_b);
  169. }
  170. */
  171.  
  172.  
  173. void test_leak() {
  174.   for (;;) {
  175.   test_Iter_Vector();
  176.   test_Iter_Association();
  177.   test_Iter_List();
  178.   test_Iter_Queue();
  179.   test_Iter_Set();
  180.   test_Iter_Bit_Set();
  181.   test_Iter_Hash_Table();
  182. //  test_Iter_Package();
  183.   }
  184. }
  185.  
  186. int main () {
  187.   START("CoolIterator");
  188.   test_Iter_Vector();
  189.   test_Iter_Association();
  190.   test_Iter_List();
  191.   test_Iter_Queue();
  192.   test_Iter_Set();
  193.   test_Iter_Bit_Set();
  194.   test_Iter_Hash_Table();
  195. #if LEAK
  196.   test_leak();
  197. #endif
  198.   SUMMARY();
  199.   return 0;
  200. }
  201.